package main

import (
	"fmt"
	"time"
)

func isValidSudoku(board [][]byte) bool {
	row := len(board)
	column := len(board[0])
	arrary := []byte{}
	arrary_two := []byte{}
	arrary_three := []byte{}

	// 行判断
	for i := 0; i < row; i++ {
		if !nonRepeat(board[i]) {
			fmt.Print("row")
			return false
		}
		// fmt.Println(string(board[i]))
	}

	// 列判断
	for j := 0; j < column; j++ {
		arrary = append(arrary[0:0])
		for i := 0; i < row; i++ {
			arrary = append(arrary, board[i][j])
			if !nonRepeat(arrary) {
				// fmt.Print("column")
				return false
			}
		}
	}

	// 3X3判断
	for i := 0; i < 9; i++ {
		if i%3 == 0 {
			// fmt.Println(arrary)
			// fmt.Println(arrary_two)
			// fmt.Println(arrary_three)
			arrary = append(arrary[0:0])
			arrary_two = append(arrary_two[0:0])
			arrary_three = append(arrary_three[0:0])
			// fmt.Println()
			// fmt.Println(arrary)
			// fmt.Println(arrary_two)
			// fmt.Println(arrary_three)

		}
		for j := 0; j < 3; j++ {
			arrary = append(arrary, board[i][j])
			arrary_two = append(arrary_two, board[i][j+3])
			arrary_three = append(arrary_three, board[i][j+6])

		}
		// fmt.Println(i)
		// fmt.Println(arrary)
		// fmt.Println(arrary_two)
		// fmt.Println(arrary_three)
		if !nonRepeat(arrary) || !nonRepeat(arrary_two) || !nonRepeat(arrary_three) {
			fmt.Print("3X3")
			return false
		}
		// fmt.Print(arrary)

	}

	return true
}

// 判断是否重复
func nonRepeat(array []byte) bool {
	remove := make(map[int]int)
	for _, vaule := range array {
		remove[int(vaule)]++
		// fmt.Printf("%s ", (string)(vaule))
		if remove[int(vaule)] > 1 && vaule != '.' {
			return false
		}
	}
	// fmt.Println()
	return true
}

func main() {
	start := time.Now()
	board := [][]byte{{'.', '2', '.', '.', '.', '.', '.', '.', '.'}, {'.', '.', '.', '.', '.', '.', '5', '.', '1'}, {'.', '.', '.', '.', '.', '.', '8', '1', '3'}, {'4', '.', '9', '.', '.', '.', '.', '.', '.'}, {'.', '.', '.', '.', '.', '.', '.', '.', '.'}, {'.', '.', '2', '.', '.', '.', '.', '.', '.'}, {'7', '.', '6', '.', '.', '.', '.', '.', '.'}, {'9', '.', '.', '.', '.', '4', '.', '.', '.'}, {'.', '.', '.', '.', '.', '.', '.', '.', '.'}}
	// fmt.Println(board)
	des := isValidSudoku(board)
	cost := time.Since(start)
	fmt.Println(des)
	fmt.Println(cost)

}
